package com.atguigu.jedis;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.*;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class JedisClient {

//    private static JedisPool jedisPool;
//    private static JedisSentinelPool jedisSentinelPool;
//    private Jedis jedis;

    private JedisCluster jedis;

    @Before
    public void before() {
//        if (jedisSentinelPool == null) {
//            Set<String> sentinels = new HashSet<>();
//            //哨兵服务所在的服务器,和我们配置的端口号
//            sentinels.add("192.168.72.103:26379");
//
//            JedisPoolConfig poolConfig = new JedisPoolConfig();
//            poolConfig.setMaxTotal(10);
//            poolConfig.setMaxIdle(5);
//            poolConfig.setMinIdle(4);
//            poolConfig.setBlockWhenExhausted(true);
//            poolConfig.setTestOnBorrow(true);
//
//            jedisSentinelPool = new JedisSentinelPool("mymaster", sentinels, poolConfig);
//        }
//        jedis = jedisSentinelPool.getResource();

        if (jedis == null) {
            JedisPoolConfig poolConfig = new JedisPoolConfig();
            poolConfig.setMaxTotal(10);
            poolConfig.setMaxIdle(5);
            poolConfig.setMinIdle(4);
            poolConfig.setBlockWhenExhausted(true);
            poolConfig.setTestOnBorrow(true);

            Set<HostAndPort> nodes = new HashSet<HostAndPort>();
            nodes.add(new HostAndPort("192.168.72.103", 6379));
            nodes.add(new HostAndPort("192.168.72.103", 6380));
            nodes.add(new HostAndPort("192.168.72.104", 6379));
            nodes.add(new HostAndPort("192.168.72.104", 6380));
            nodes.add(new HostAndPort("192.168.72.105", 6379));
            nodes.add(new HostAndPort("192.168.72.105", 6380));
            jedis = new JedisCluster(nodes, poolConfig);
        }
    }

    @After
    public void after() {
        jedis.close();
    }

    @Test
    public void string() {
        jedis.set("aaa", "zhangsan");
        System.out.println(jedis.get("aaa"));
//        jedis.mset("bbb", "lisi", "ccc", "wangwu");
//        System.out.println(jedis.mget("aaa", "bbb", "ccc"));
//        System.out.println(jedis.incr("aaa"));
//        String set = jedis.getSet("aaa", "zhangsan");
//        System.out.println(set);
    }

    @Test
    public void list() {
        jedis.rpush("mylist", "aaa", "bbb", "ccc", "ddd");
        System.out.println(jedis.lrange("mylist", 0, -1));
        System.out.println(jedis.lpop("mylist"));
        System.out.println(jedis.lrange("mylist", 0, -1));
    }

    @Test
    public void set() {
//        jedis.sadd("myset", "aaa", "bbb", "ccc");
        jedis.sadd("myset2", "aaa", "ddd", "eee");
        System.out.println(jedis.sinter("myset", "myset2"));
        System.out.println(jedis.sunion("myset", "myset2"));
        System.out.println(jedis.sdiff("myset", "myset2"));

    }

    @Test
    public void hash() {
//        Map<String, String> map = new HashMap<>();
//        map.put("name", "zhangsan");
//        map.put("age", "18");
//        map.put("gender", "male");
//        jedis.hset("zhangsan", "name", "zhangsan");
//        jedis.hset("zhangsan", "age", "18");
//        jedis.hset("zhangsan", "gender", "male");

//        System.out.println(jedis.hget("zhangsan", "name"));
        Map<String, String> zhangsan = jedis.hgetAll("zhangsan");
        System.out.println(zhangsan);
    }

    @Test
    public void zset() {
//        jedis.zadd("myzset", 97, "zhangsan");
//        jedis.zadd("myzset", 98, "lisi");
//        jedis.zadd("myzset", 98, "wangwu");
//        jedis.zadd("myzset", 99, "zhaoliu");

        Map<String, Double> m = new HashMap<String, Double>();
        m.put("zhangsan", 97.0);
        m.put("lisi", 98.0);
        m.put("wangwu", 99.0);
        m.put("zhaoliu", 100.0);

        jedis.zadd("myzset", m);

        System.out.println(jedis.zrange("myzset", 0, -1));
    }

    @Test
    public void generic() throws InterruptedException {
//        Set<String> keys = jedis.keys("*");
//        System.out.println(jedis.exists("myzset"));
//        jedis.expire("myzset", 300);
//        System.out.println(jedis.ttl("myzset"));
//        Thread.sleep(10000);
//        System.out.println(jedis.ttl("myzset"));
//        jedis.persist("myzset");
//        System.out.println(jedis.ttl("myzset"));
//        System.out.println(jedis.dbSize());
//        jedis.flushDB();
//        System.out.println(jedis.dbSize());
    }




}
